<template>
  <div class="number-container d-flex justify-content-center align-items-center">
    <!-- 减 1 的按钮 -->
    <button type="button" class="btn btn-light btn-sm" @click="reduceCounter">-</button>
    <!-- 购买的数量 -->
    <span class="number-box">{{ count }}</span>
    <!-- 加 1 的按钮 -->
    <button type="button" class="btn btn-light btn-sm" @click="addCounter">+</button>
  </div>
</template>

<script>

export default {
  props: ['Count', 'id'],
  data() {
    return {
      count: this.Count
    }
  },
  methods: {
    addCounter() {
      if (this.count < 100) {
        this.count++
      }
    },
    reduceCounter() {
      if (this.count > 0) {
        this.count--
      }
    },
  },
  watch: {
    count(newVal, oldVal) {
      this.$store.commit('countChange', { id: this.id, count: this.count })
    }
  }
}

</script>

<style lang="less" scoped>
.number-box {
  min-width: 30px;
  text-align: center;
  margin: 0 5px;
  font-size: 12px;
}

.btn-sm {
  width: 30px;
}
</style>
